/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO : Think about improvements
package martian.m4gic.services
{
	import flash.display.*;
	import flash.events.*;
	
	import martian.m4gic.services.Service;
	import martian.m4gic.tools.Domain;
	
	public class Enterframe extends Service
	{
		public function Enterframe() { super(); }
		
		/* service name */
		static private const NAME:String = "Enterframe";
		
		/* service properties */
		static public const PLAYING:int = 0;
		static public const PAUSED:int = 1;
		static public const STOPPED:int = 2;
		
		
		
		static private var f:int = 0;
			static public function get frame():int { return f; }
		
		static private var s:int = 2;
			static public function get status():int { return s; }
			
		static private var functions:Vector.<Function>;
			static private var l:int = 0;
			static public function get length():int { return l; }
			
			static public function get tick():Number { return 1/framerate; }
			static public function get framerate():int { return stage.frameRate; }
			static public function set framerate(value:int):void { stage.frameRate = value; }
			
		/* service initialization */
		static private function _register(args:Object = null):void
		{
			functions = new Vector.<Function>();
		}
		
		static private function _release(args:Object = null):void
		{
			functions = null;
		}
		
		static private function _activate(args:Object = null):void
		{
			if (s != PLAYING) { play(); }
		}
		
		static private function _desactivate(args:Object = null):void
		{
			if (s == PLAYING) { stop(); }
		}
		
		/* service methods */
		static public function add(func:Function):Function
		{
			if (registered)
			{
				functions.push(func);
				l++;
				
				return func;
			}
			
			return null;
		}
		
		static public function insert(func:Function, index:int):Function
		{
			if (registered)
			{
				functions.splice(index, 0, func);
				l++;
				
				return func;
			}
			
			return null;
		}
		
		static public function swap(item1:*, item2:*):Boolean
		{
			if (registered)
			{
				var i:int = 0;
			
				if (Domain.identify(item1) == "Function")
				{
					for (i = 0; i < functions.length; i++) { if (item1 == functions[i]) { item1 = i; break; } }
				} else if (Domain.identify(item1) != "int") { throw new Error('wrong argument type item1 in Enterframe.swap()'); }
				
				if (Domain.identify(item2) == "Function")
				{
					for (i = 0; i < functions.length; i++) { if (item2 == functions[i]) { item2 = i; break; } }
				} else if (Domain.identify(item2) != "int") { throw new Error('wrong argument type item2 in Enterframe.swap()'); }
				
				var tmp:* = functions[item1];
				functions[item1] = functions[item2];
				functions[item2] = tmp;
				
				return true;
			}
			
			return false;
		}
		
		static public function remove(item:*):Boolean
		{
			if (registered)
			{
				var type:String = Domain.identify(item);
				
				if (Domain.identify(item) == "Function")
				{
					for (var i:int = 0; i < functions.length; i++) { if (item == functions[i]) { item = i; break; } }
				} else if (Domain.identify(item) != "int") { throw new Error('wrong argument type item in Enterframe.remove() [' + typeof(item) + ']'); }			
				
				functions.splice(item, 1);
				l--;
				
				return true;
			}
			
			return false;
		}
		
		static public function flush():Boolean
		{
			if (registered)
			{
				functions = new Vector.<Function>;
				l = 0;
				
				return true;
			}
			
			return false;
		}
		
		static public function play():void
		{
			if (registered && s != 0)
			{
				if (s == STOPPED) { f = 0; }
				
				stage.addEventListener(Event.ENTER_FRAME, enterframe);
				s = PLAYING;
			}
		}
		
		static public function pause():void
		{
			if (registered && s == 0)
			{
				stage.removeEventListener(Event.ENTER_FRAME, enterframe);
				s = PAUSED;
			}			
		}
		
		static public function stop():void
		{
			if (registered && s < 2)
			{
				if (s == 0) { pause(); }
				s = STOPPED;
			}
		}
		
		static public function step():void { for (var i:int = 0; i < length; i++) { functions[i].call(null); } }
		
		static private function enterframe(e:Event):void
		{
			step();
			f++;
		}
		
		
		
		/* inheritance */
		static private var reference:Sprite;
		static private var dispatch:EventDispatcher;
		static private var stage:Stage;
		
		static private var registered:Boolean = false;
		static private var activated:Boolean = false;
			static public function get available():Boolean { return (registered && activated); }
		
		static public function register(container:Sprite, args:Object = null):void
		{
			if (!registered)
			{
				reference = container;
				dispatch = new EventDispatcher();
				stage = reference.stage;
				
				_register(args);
				
				registered = true;
				
				if (args != null) { if (args.active != undefined) { if (args.active == true) { activate(); } } }
			}
			else { trace("6:", NAME, "already registered."); }
		}
		
		static public function release(args:Object = null):void
		{
			if (registered && !activated)
			{
				stage = null;
				dispatch = null;
				reference = null;
				
				_release(args);
				
				registered = false;
			}
			else { trace("6:", NAME, "already released."); }
		}
		
		static public function activate(args:Object = null):void
		{
			if (registered && !activated)
			{
				activated = true;
				_activate(args);
			}			
		}
		
		static public function desactivate(args:Object = null):void
		{
			if (registered && activated)
			{
				activated = false;
				_desactivate(args);
			}
		}
		
		static private function dispatchEvent(event:Event):void
		{
			dispatch.dispatchEvent(event);
		}
		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			dispatch.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			dispatch.removeEventListener(type, listener, useCapture);
		}
	}
}